package com.chl.httpproxyserver;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class HttpProxyServer {
	public static void main(String[] args) throws IOException {
		try {
			String proxyHost = "192.168.0.211";
			int remotePort = 8080;
			int localPort = 8888;
			// Printing the start-up message
			System.out.println("Starting proxy for " + proxyHost + ":" + remotePort + " on port " + localPort);
			// start the server
			runServer(proxyHost, remotePort, localPort);
		} catch (Exception e) {
			System.err.println(e);
		}
	}

	public static void runServer(String proxyHost, int remotePort, int localPort) throws IOException {
		// Create a ServerSocket to listen connections
		ServerSocket serverSocket = new ServerSocket(localPort);
		final byte[] request = new byte[1024];
		byte[] reply = new byte[4096];
		while (true) {
			Socket socketClient = null, socketServer = null;
			try {
				// wait for a connection on the local port
				socketClient = serverSocket.accept();
				InputStream inputStreamClient = socketClient.getInputStream();
				OutputStream outputStreamClient = socketClient.getOutputStream();
				// Create the connection to the real server.
				try {
					socketServer = new Socket(proxyHost, remotePort);
				} catch (IOException e) {
					PrintWriter out = new PrintWriter(outputStreamClient);
					out.print(
							"The Proxy Server could not connect to " + proxyHost + ":" + remotePort + ":\n" + e + "\n");
					out.flush();
					socketClient.close();
					continue;
				}
				InputStream inputStreamServer = socketServer.getInputStream();
				OutputStream outputStreamServer = socketServer.getOutputStream();
				// The thread to read the client's requests and to pass them
				Thread newThread = new Thread() {
					public void run() {
						int bytesRead;
						try {
							while ((bytesRead = inputStreamClient.read(request)) != -1) {
								outputStreamServer.write(request, 0, bytesRead);
								outputStreamServer.flush();
							}
						} catch (IOException e) {
							System.err.println(e);
						}
						// Close the connections
						try {
							outputStreamServer.close();
						} catch (IOException e) {
							System.err.println(e);
						}
					}
				};
				// client-to-server request thread
				newThread.start();
				// Read server's responses and pass them to the client.
				int bytesRead;
				try {
					while ((bytesRead = inputStreamServer.read(reply)) != -1) {
						outputStreamClient.write(reply, 0, bytesRead);
						outputStreamClient.flush();
					}
				} catch (IOException e) {
					System.err.println(e);
				}
				// Close the connection
				outputStreamClient.close();
			} catch (IOException e) {
				System.err.println(e);
			} finally {
				try {
					if (socketServer != null)
						socketServer.close();
					if (socketClient != null)
						socketClient.close();
				} catch (IOException e) {
					System.err.println(e);
				}
			}
		}
	}
}